package com.dyrj.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;


/**
 * 阿里云短信接口
 * @blue
 */
public class SmsUtil {

    public static final String REGION = "cn-hangzhou";
    public static final String ACCESS_KEY_ID = "LTAI4GA18ncrPtiZGunfUT1U";
    public static final String ACCESS_SECRET = "md9ddu4sAdGj3qBOxQ5vUnh4AWR0Fn";
    public static final String DOMAIN = "dysmsapi.aliyuncs.com";
    public static final String VERSION = "2017-05-25";
    public static final String SIGN_NAME = "乐彩农牧生猪采购平台";
    public static final String TEMPLATE_CODE = "SMS_197895900";

    public static final String KEY="79021de04317b1a5e0037c4b3d4fac05";//sms.cn

    /**
     * 发送短信
     * @param phone 手机号
     * @param code 模板code
     */
    public static boolean sendSms(String phone,String code){
        DefaultProfile profile = DefaultProfile.getProfile(REGION, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", REGION);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String json = response.getData();
            if(json.contains("OK")){
                return true;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean sendYjdtSms(String phone,String name,String code){

        String url = "http://api.sms.cn/sms/?ac=send&uid=zhaoxuechao&pwd="+KEY+
                "&template=548642&mobile="+phone+"&content=";
//        String content = "{\"code\":\""+code+"\",\"name\":\""+phone+"\"}";
        String content = "{\"name\":\""+name+"\",\"code\":\""+code+"\"}";
        content = URLEncoder.encode(content);
        url = url + content;
        System.out.println("url："+url);
        String result = doGet(url);
        System.out.println("短信发送："+result);
        if(result != null && result.contains("发送成功")){
            return true;
        }

        return false;
    }
    public static String doGet(String url) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
//            httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
            // 设置配置请求参数
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)
                    .setConnectionRequestTimeout(35000)
                    .setSocketTimeout(60000)
                    .build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    public static void main(String[] args) {
        sendYjdtSms("18642719358","刘超","66666");
    }
}
